package com.npf.imgserver.utils;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.nio.charset.Charset;
import java.util.Map;

/**
 * 〈功能简述〉
 * 〈http调用 工具类〉
 *
 * @author NiPengFei
 * @create 2018/6/4
 * @since 1.0.0
 */
public class HttpUtil {
    /**
     * Content-Type: application/x-www-form-urlencoded
     *
     * @param url
     * @param map
     * @return
     * @throws Exception
     */
    public static String post(String url, Map<String, String> map) throws Exception {

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();

        for (Map.Entry<String, String> entry : map.entrySet()) {
            body.add(entry.getKey(), entry.getValue());
        }

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(body, headers);
        try {
            return restTemplate.postForObject(url, entity, String.class);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * Content-Type: application/json
     *
     * @param url
     * @param requestJson
     * @return
     * @throws Exception
     */
    public static String post(String url, String requestJson) throws Exception {

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
        try {
            return restTemplate.postForObject(url, entity, String.class);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 带请求超时的 post请求调用
     *
     * @param url
     * @param requestJson
     * @param timeout
     * @return
     * @throws Exception
     */
    public static String post(String url, String requestJson, int timeout) throws Exception {

        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(timeout);
        httpRequestFactory.setConnectTimeout(timeout);
        httpRequestFactory.setReadTimeout(timeout);

        RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
        try {
            return restTemplate.postForObject(url, entity, String.class);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * get 请求
     *
     * @param url
     * @param map
     * @param timeout
     * @return
     * @throws Exception
     */
    public static String get(String url, Map<String, String> map, int timeout) throws Exception {

        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(timeout);
        httpRequestFactory.setConnectTimeout(timeout);
        httpRequestFactory.setReadTimeout(timeout);

        RestTemplate restTemplate = new RestTemplate(httpRequestFactory);

        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();

        for (Map.Entry<String, String> entry : map.entrySet()) {
            params.add(entry.getKey(), entry.getValue());
        }

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).queryParams(params);
        try {
            return restTemplate.getForObject(builder.build(false).encode().toUri(), String.class);

        } catch (Exception e) {
            throw e;
        }

    }

}
